﻿using IntelligentAgriculture.Contracts.Interface.ColumnManagements;
using IntelligentAgriculture.Contracts.Interface.EarTagService;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Services.ColumnManagementService;
using IntelligentAgriculture.WebAPI.Application.Command.ColumnManageCommand;
using IntelligentAgriculture.WebAPI.Application.Command.EarTageCommand;
using MediatR;

namespace IntelligentAgriculture.WebAPI.Application.CommandHandler.ColumnManageCommandJamdler
{
    public class EarTageAddCommandHandler : IRequestHandler<EarTageAddCommand, Results<int>>
    {
        private readonly IEarTageService iearTageService;

        public EarTageAddCommandHandler(IEarTageService iearTageService)
        {
            this.iearTageService = iearTageService;
        }
        /// <summary>
        /// 执行耳标添加操作
        /// </summary>
        /// <param name="request">包含耳标添加所需的请求信息</param>
        /// <param name="cancellationToken">用于取消操作的令牌</param>
        /// <returns>返回一个表示耳标添加操作结果的任务</returns>
        public async Task<Results<int>> Handle(EarTageAddCommand request, CancellationToken cancellationToken)
        {
            var entity = new Eartag()
            {
                EarTagBirthStatus = request.EarTagBirthStatus,
                EarTagUseStatus = request.EarTagUseStatus,
                BeforNumber = request.BeforNumber,
                AfterNumber = request.AfterNumber,
                EarTagBreeds = request.EarTagBreeds,
                EarTagNumber = request.EarTagNumber,
                EarTagInHouseDate=DateTime.Now,
                Isdel=false
            };

            try
            {
                var result = await iearTageService.Add(entity);
                return Results<int>.DataResult(result);
            }
            catch (Exception ex)
            {
                // 处理异常，比如记录日志等
                Console.WriteLine($"耳标添加操作发生异常: {ex.Message}");
                throw; // 可以选择抛出异常继续传播
            }
        }

   
        
    }
}
